<!--
+----------------------------------------------------------------------
| 友得云客  - 开启房产营销新纪元
+----------------------------------------------------------------------
| Copyright (c) 2019~2023 优得（西安）信息科技有限公司版权所有
+----------------------------------------------------------------------
| Licensed 友得云客不是自由软件 未经允许不可移除相关版权
+----------------------------------------------------------------------
| Author: www.youdeyunke.com
+----------------------------------------------------------------------
-->
<template>
    <el-dialog :visible.sync="show" :destroy-on-close="true" title="商品规格管理" width="600px" :close-on-click-modal="false">
        <div class="items" v-cloak v-loading="loading">
            <div v-if="showForm == false">
                <el-table :data="items" v-loading="loading">
                    <el-table-column label="规格名称" prop="standards"></el-table-column>
                    <el-table-column label="规格库存" prop="stock"></el-table-column>
                    <el-table-column label="规格积分" prop="score"></el-table-column>
                    <el-table-column label="操作" width="80">
                        <template slot-scope="scope">
                            <el-link @click="editHandle(scope.row)">编辑</el-link>
                            <el-link type="danger" @click="deleteHandle(scope.row)">删除</el-link>
                        </template>
                    </el-table-column>
                </el-table>

                <el-button style="margin:10px auto 0 auto; width:100%;" type="primary" plain round
                    icon="el-icon-circle-plus" @click="createHandle">新增规格</el-button>
            </div>

            <standard-form @change="changeHandle" v-else :currentItem="currentItem" :is_virtual="is_virtual"></standard-form>
        </div>
    </el-dialog>
</template>

<script>
import { getGoodsStandards, deleteGoodsStandards } from "@/api/score-good";
import standardForm from "./standardForm";
import PublicStatus from "@/components/PublicStatus";

export default {
    components: { standardForm, PublicStatus },
    data() {
        return {
            items: [],
            currentItem: null,
            showForm: false,
            loading: false,
            show: false,
            goodid: null,
        };
    },
    watch: {
        show: function (newVal, oldVal) {
            if (newVal == false) {
                this.currentItem = null;
                this.showForm = false;
                this.items = [];
                this.goodid = null;
                this.$emit("change", {});
            }
        },
    },
    props: {
        is_virtual: { type: Boolean, default: false}
    },

    mounted: function () {
        this.loadData();
    },

    computed: {},

    methods: {

        openDialog: function (id) {
            // this.currentItem = item;
            this.goodid = id;
            this.loadData()
            this.show = true;
        },

        changeHandle: function () {
            this.showForm = false;
            this.loadData();
        },

        deleteHandle: function (item) {
            this.$confirm(
                "确定要删除该商品规格吗？"
            ).then(() => {
                deleteGoodsStandards(item.id).then(resp => {
                    if (resp.status != 0) {
                        return;
                    }
                    this.loadData();
                });
            });
        },

        editHandle: function (item) {
            this.currentItem = item;
            this.showForm = true;
        },
        createHandle: function () {
            var item = {
                goods_id: this.goodid, 
                standards: '',
                image: '',
                stock: 0,
                score: 0,
                amount: 0,
            };
            this.currentItem = item;
            this.showForm = true;
        },

        loadData: function () {
            var id = this.goodid;
            if(!id) { return } 
            this.loading = true;
            getGoodsStandards(id).then(resp => {
                this.loading = false;
                if (resp.status != 0) {
                    return;
                }
                this.items = resp.data
            });
        }
    }
};
</script>

<style scoped></style>